package main

import (
	"image/color"
	"log"

	"github.com/ajhager/spine"
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)

type Game struct {
	skeleton  *spine.Skeleton
	animation *spine.AnimationState
}

func NewGame() *Game {
	// 加载 Spine 数据
	atlas, err := spine.NewAtlas("path/to/your.atlas", "path/to/your.png")
	if err != nil {
		log.Fatalf("failed to load atlas: %v", err)
	}
	defer atlas.Dispose()

	json, err := spine.New(atlas)
	if err != nil {
		log.Fatalf("failed to load skeleton json: %v", err)
	}

	skeletonData := json.ReadSkeletonData("your_skeleton.json")
	skeleton := spine.NewSkeleton(skeletonData)

	animationStateData := spine.NewAnimationStateData(skeletonData)
	animation := spine.New(animationStateData)

	// 设置初始动画
	animation.SetAnimation(0, "your_animation", true)

	return &Game{
		skeleton:  skeleton,
		animation: animation,
	}
}

func (g *Game) Update() error {
	g.animation.Update(1.0 / 60.0) // 更新动画
	g.skeleton.UpdateWorldTransform()
	return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
	screen.Fill(color.RGBA{255, 255, 255, 255}) // 清屏
	g.skeleton.Draw(screen)
}

func (g *Game) Layout(int, int) (int, int) {
	return 800, 600 // 窗口大小
}

func main() {
	game := NewGame()
	ebiten.SetWindowSize(800, 600)
	ebiten.SetWindowTitle("Spine Animation with Ebiten")
	if err := ebiten.RunGame(game); err != nil {
		log.Fatalf("failed to run game: %v", err)
	}
}
